package com.cp.helper.controller;

import com.cp.helper.bean.Todo;
import com.cp.helper.dao.impl.TodoDao;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @描述:
 * @Author : chenpeng
 * @Date : 2015/10/22 20:53
 */
@Controller
@RequestMapping("/todo")
public class TodoController {

    @Autowired
    private TodoDao todoDao;

    @RequestMapping("/add_todo")
    @ResponseBody
    public Object addTodo(HttpServletRequest request) throws Exception {
        Map<String, Object> map = new HashMap<String, Object>();
        int userId = 1;
        String content = request.getParameter("content");
        if (StringUtils.isEmpty(content)) {
            map.put("success", false);
            return map;
        }

        Todo todo = new Todo();
        todo.setContent(content);
        todo.setStatusFlag(1);
        todo.setUserId(userId);
        todoDao.insert(todo);

        map.put("success", true);
        map.put("todo", todo);
        return map;
    }


    @RequestMapping("/get_todo_list")
    @ResponseBody
    public Object getTodoList(HttpServletRequest request) throws Exception {
        Map<String, Object> map = new HashMap<String, Object>();

        int userId = 1;
        Map<String, Object> qryMap = new HashMap<String, Object>();
        qryMap.put("userId", userId);
        qryMap.put("statusFlag", 1);

        List<Todo> todoList = todoDao.selectList(qryMap);
        qryMap.put("statusFlag", 2);
        List<Todo> doneList = todoDao.selectList(qryMap);

        map.put("todoList", todoList);
        map.put("doneList", doneList);
        map.put("success", true);

        return map;
    }

    @RequestMapping("/finish_todo")
    @ResponseBody
    public Object finishTodo(HttpServletRequest request) throws Exception {
        Map<String, Object> map = new HashMap<String, Object>();

        int todoId = Integer.valueOf(request.getParameter("todoId"));

        Todo todo = todoDao.selectByKey(todoId);
        todo.setStatusFlag(2);
        todoDao.updateStatusFlagById(todo);

        map.put("success", true);
        return map;
    }
}
